﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntityObject;
using ThangNM_LIB.FUNC;
using Facebook;
using System.Collections;
using System.Data.Sql;
using System.IO;
using ThangNM_Client.DataAccessObject;
using System.Text.RegularExpressions;
using System.Net;
using System.Web;
using HtmlAgilityPack;
using Newtonsoft.Json;
using DataAccessObject;
using ThangNM_LIB.DB;
using ThangNM_LIB.EO;

namespace ThangNM_Client
{
    public partial class frmTestFacebookApp : Form
    {
        public frmTestFacebookApp()
        {
            InitializeComponent();
            System.Threading.Thread.Sleep(1000);
            //txtRet.Text = "https://www.facebook.com/officesurvivalguide/ab/videos/1557368117924354/vd/3.aps";//"262997240494205    ";
        }


        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                KeyValueEO _kv = new KeyValueEO();
                _kv = OtherEx.ConvertNumberToChar(_kv, 9);
                txtRet.Text = _kv.K + "/" + _kv.V;
                //txtRet.Text = OtherEx.getRootPath();
                return;
                OtherEx.getHttp_Status("http://33a90e29250c3ca3de49f1d7cf0bbe15.lswcdn.net/video_hq/183/kNuSdzBIxw/video_hq.mp4?st=jfmedto9oF6hIX4gD7GKLQ&e=1458929208&ri=2600&rs=256");
                PHDEO _PHDEO;
                players _players;
                String sHtmlContent = "";
                String start = "players.push({", end = "});";
                Int32 startIndex = 0;
                Int32 endIndex = 0;
                String sUrlBase = "http://nguyenmanhthang.somee.com/FacebookProxy.ashx?x={0}";
                for (Int32 i = 1; i <= 50000; i++)
                {
                    try
                    {
                        using (WebClient client = new WebClient())
                        {
                            client.Encoding = Encoding.UTF8;
                            client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36");
                            sHtmlContent = client.DownloadString(String.Format(sUrlBase, i));
                        }
                        startIndex = sHtmlContent.IndexOf(start) + start.Length;
                        endIndex = sHtmlContent.IndexOf(end, startIndex);
                        String sJsonData = "{" + sHtmlContent.Substring(startIndex, endIndex - startIndex) + "}";
                        sJsonData = sJsonData.Replace("'' ===", "");

                        _players = new players();
                        _players = (players)JsonEx.Json2Object(sJsonData, _players.GetType());
                        if (_players != null)
                        {
                            _PHDEO = new PHDEO(_players);

                            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                            doc.LoadHtml(sHtmlContent);
                            HtmlNode meta_tag;
                            meta_tag = doc.DocumentNode.SelectSingleNode("//head//meta[@name='og:image']");
                            if (meta_tag != null)
                                _PHDEO.sImageSmall = ConvertEx.SQLite.RemoveSpecialCharacter(meta_tag.Attributes["content"].Value);

                            meta_tag = doc.DocumentNode.SelectSingleNode("//head//meta[@name='description']");
                            if (meta_tag != null)
                                _PHDEO.sDescription = ConvertEx.SQLite.RemoveSpecialCharacter(meta_tag.Attributes["content"].Value);

                            meta_tag = doc.DocumentNode.SelectSingleNode("//head//title");
                            if (meta_tag != null)
                                _PHDEO.sTitle = ConvertEx.SQLite.RemoveSpecialCharacter(meta_tag.InnerText);

                            meta_tag = doc.DocumentNode.SelectSingleNode("//head//link[@rel='canonical']");
                            if (meta_tag != null)
                                _PHDEO.sUrl = ConvertEx.SQLite.RemoveSpecialCharacter(meta_tag.Attributes["href"].Value);

                            (new PHDDAO()).Insert_Update_Delete(_PHDEO, ConstEx.Insert_Update_Delete.Insert);
                        }
                    }
                    catch (Exception ex)
                    {
                        (new Logs()).WriteLog_To_DB(new ThangNM_LIB.EO.LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false);
                    }
                }
                return;

                //txtRet.Text = txtRet.Text + "_" + OtherEx.Facebook_UserName_To_UserID(txtRet.Text);
                //return;
                //String htmlString = "";
                //String user_name = txtRet.Text;
                //Int64 UID = 0;
                //using (WebClient client = new WebClient())
                //{
                //    //client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36");
                //    htmlString = client.DownloadString("https://m.facebook.com/" + user_name);
                //    MatchCollection mclTemp = Regex.Matches(htmlString, "(?<=\"entity_id\":)(\\d+)", RegexOptions.Singleline);
                //    htmlString = StringEx.TrimEx((mclTemp.Count > 0 ? mclTemp[0].Groups[0].Value : null));
                //    Int64.TryParse(htmlString, out UID);
                //}
                //txtRet.Text = user_name + "_" + UID;

                //return;

                //txtRet.Text = HttpUtility.UrlEncode(txtRet.Text);
                //using (WebClient client = new WebClient())
                //{
                //    if (OtherEx.getAppSettings("FacebookProxy_Proxy", "9").Equals("1"))
                //    {
                //        WebProxy proxy = new WebProxy("proxy.fis.vn", 8080);
                //        proxy.Credentials = new NetworkCredential("fis\\ThangNM11", "AbCd@1234");
                //        proxy.UseDefaultCredentials = false;
                //        proxy.BypassProxyOnLocal = true;
                //        client.Proxy = proxy;
                //    }
                //    String strJson = client.DownloadString("https://fb.com/" + "thang.991992");
                //    Int64.TryParse(strJson, out UID);
                //}
                //return;

                //FacebookApp.SharedLibraries.ScheduleJobs.btnCheck_Click("10", "it@gmail.com", " https://www.facebook.com/share2everyone/posts/784604151666842");
                //return;



                //MessageBox.Show(JsonEx.Object2Json(objRequest));
                //return;


                //String text = File.ReadAllText("c:\\sv_email.txt");
                //using (StringReader sr = new StringReader(text))
                //{
                //    string line;
                //    while ((line = sr.ReadLine()) != null)
                //    {
                //        try
                //        {
                //            (new BuildWebDAO()).DuongNTH_InsertItem(line, EmailEx.CheckExists_Email(line));
                //        }
                //        catch (Exception ex)
                //        {
                //            (new Logs()).WriteLog_To_DB(new ThangNM_LIB.EO.LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false);
                //        }
                //    }
                //}
                // String a = (new Microsoft.SqlServer.Management.Smo.Agent.Job()).Name;

                //StringBuilder sb = new StringBuilder();
                //DataTable dataTable = Microsoft.SqlServer.Management.Smo.SmoApplication.EnumAvailableSqlServers(true);
                //txtRet.Text = JsonEx.Object2Json(dataTable);
                //return;

                //System.Data.Sql.SqlDataSourceEnumerator

                //SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
                //System.Data.DataTable table = instance.GetDataSources();
                //foreach (System.Data.DataRow row in table.Rows)
                //{
                //    if (row["ServerName"] != DBNull.Value && Environment.MachineName.Equals(row["ServerName"].ToString()))
                //    {
                //        string item = string.Empty;
                //        item = row["ServerName"].ToString();
                //        if (row["InstanceName"] != DBNull.Value || !string.IsNullOrEmpty(Convert.ToString(row["InstanceName"]).Trim()))
                //        {
                //            item += @"\" + Convert.ToString(row["InstanceName"]).Trim();
                //        }
                //        sb.Append(item + "\n");
                //    }
                //}
                //txtRet.Text = sb.ToString();
                //return;

                //SortedList sl = new SortedList();
                //sl.Add("1", "A");
                //sl.Add("2", "B");
                //sl.Add("3", "C");
                //sl.Add("4", "D");
                //txtRet.Text = GetListConstantsEx.FindValue_ByKey(sl, txtRet.Text);
                //return;
                //txtRet.Text = "262997240494205";
                Facebook.FacebookClient FBClient = new FacebookClient();
                FBClient.AppId = OtherEx.getAppSettings("Facebook_AppId");
                FBClient.AppSecret = OtherEx.getAppSettings("Facebook_AppSecret");
                FBClient.AccessToken = "CAAXaKZCf3qnkBAB0GyTyozEBwHq2DEEhokYxZCNmlRq05FynZAzVwBFvvGES68TuTnJ2IO32XQuC46BZC1clN7bId4CsBEDgnnmYzM6JBq60njv7uAA0dNLCbwbbLpfch0qIlzzHuuurDZBabmoCDcFsK8bv6Dx7diTVWu9JxQvvWvwhBkeMp";
                FacebookOptionEO objRet = new FacebookOptionEO();
                objRet = (new FacebookApp.SharedLibraries.ScheduleJobs.Facebook_Object()).User_Page_Group(FBClient, new FacebookOptionEO(ConstEx.Facebook_Type.unknown, "", "share2everyone", "", "", true, true, true, true, true, false));
                //objRet = (new FacebookApp.SharedLibraries.ScheduleJobs.Facebook_Object()).Post_Photo_Video_Link_Event(FBClient, new FacebookOptionEO(0, txtRet.Text));
                txtRet.Text = JsonEx.Object2Json(objRet);
                //txtRet.Text = OtherEx.Facebook_Graph_API_Get(FBClient, "me");

                //(new FacebookApp.SharedLibraries.ScheduleJobs()).evttimerAutoScan(new System.Timers.Timer(), null);
                //(new ThangNM_WinSV.ProjectInstaller())..SharedLibraries.ScheduleJobs()).evttimerAutoScan(new Timer(), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnTestSQLite_Click(object sender, EventArgs e)
        {
            List<String> lstTemp = new List<string>();
            for (int i = 0; i <= 1000; i++)
            {
                lstTemp.Add(Guid.NewGuid().ToString());
            }
            //txtRet.Text = txtRet.Text + "\n\n- " + (new SqliteBulkCopy()).Insert_UsingTran_1(lstTemp);

            txtRet.Text = txtRet.Text + "\n\n- " + (new SqliteBulkCopy()).Insert_UsingTran_2(lstTemp);
        }
    }
}
